package com.fysports.web.module.view;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.json.Json;

import java.util.Collection;
import java.util.Map;


/**
 * Json vo 对象
 * Date : 14-8-14 上午10:49
 * Project : JdmData_WebServer
 */
public class JsonView {
    private Map<String, Object> data = Maps.newHashMap();
    private boolean status = false;
    private Map<String, Object> _def_data = Maps.newHashMap();
    private Object dataObj;
    private Collection dataList = Lists.newArrayList();
    private String msg;
    private StatusCode statusCode = StatusCode.DEFAULT;


    public JsonView() {
    }


    public JsonView(boolean status, OperateType operateType) {
        this.status = status;
        this.msg = operateType.getStrVal();
    }


    public JsonView(boolean status, String message) {
        this.status = status;
        this.msg = message;
    }


    public String getMsg() {
        return msg;
    }


    public JsonView setMsg(String msg) {
        this.msg = msg;
        return this;
    }


    public JsonView setMsg(OperateType operateType) {
        this.msg = operateType.getStrVal();
        return this;
    }


    public boolean isStatus() {
        return status;
    }


    public JsonView setStatus(boolean status) {
        this.status = status;
        return this;
    }


    public Object getDataObj() {
        return dataObj;
    }


    public JsonView setDataObj(Object dataObj) {
        this.dataObj = dataObj;
        return this;
    }


    public JsonView setMapEntry(String key, Object obj) {
        _def_data.put(key, obj);
        return this;
    }


    public Collection getDataList() {
        return dataList;
    }


    public StatusCode getStatusCode() {
        return statusCode;
    }


    public JsonView setStatusCode(StatusCode statusCode) {
        this.statusCode = statusCode;
        return this;
    }


    public JsonView setDataList(Collection dataList) {
        this.dataList = dataList;
        return this;
    }


    public String toJson() {
        data.put("status", status);
        data.put("msg", msg);
        if (dataObj == null) {
            data.put("dataObj", _def_data);
        }
        else {
            data.put("dataObj", dataObj);
        }
        data.put("dataList", dataList);
        data.put("statusCode", statusCode.toString());
        return Json.getJson().toJson(data);
    }


    /**
     * API二级状态预留对象；
     * 用来标示特殊状态；
     */
    public enum StatusCode {
        /**
         * 默认
         */
        DEFAULT(100, "nothing");


        StatusCode(Integer intv, String strv) {
            this.intval = intv;
            this.strval = strv;
        }


        private Integer intval;
        private String strval;


        public String toString() {
            Map<String, Object> m = Maps.newHashMap();
            m.put("code", this.intval);
            m.put("codemsg", this.strval);
            return Json.getJson().toJson(m);
        }
    }


    public static void main(String[] a) {
//                Map data= Maps.newHashMap();
//                data.put("statusCode", StatusCode.CODE_TEST1);
        //        System.out.println(StatusCode.CODE_TEST1);
//                System.out.println(JsonKit.toJson(new JsonView(),1000));
    }
}
